package me.zhengjie.demo.service.impl;

import me.zhengjie.demo.domain.Demo;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.demo.repository.DemoRepository;
import me.zhengjie.demo.service.DemoService;
import me.zhengjie.demo.service.dto.DemoDto;
import me.zhengjie.demo.service.dto.DemoQueryCriteria;
import me.zhengjie.demo.service.mapper.DemoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ZhengGuozhen
* @date 2020-04-10
*/
@Service
//@CacheConfig(cacheNames = "demo")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DemoServiceImpl implements DemoService {

    private final DemoRepository demoRepository;

    private final DemoMapper demoMapper;

    public DemoServiceImpl(DemoRepository demoRepository, DemoMapper demoMapper) {
        this.demoRepository = demoRepository;
        this.demoMapper = demoMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(DemoQueryCriteria criteria, Pageable pageable){
        Page<Demo> page = demoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(demoMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<DemoDto> queryAll(DemoQueryCriteria criteria){
        return demoMapper.toDto(demoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public DemoDto findById(Integer id) {
        Demo demo = demoRepository.findById(id).orElseGet(Demo::new);
        ValidationUtil.isNull(demo.getId(),"Demo","id",id);
        return demoMapper.toDto(demo);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public DemoDto create(Demo resources) {
        return demoMapper.toDto(demoRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(Demo resources) {
        Demo demo = demoRepository.findById(resources.getId()).orElseGet(Demo::new);
        ValidationUtil.isNull( demo.getId(),"Demo","id",resources.getId());
        demo.copy(resources);
        demoRepository.save(demo);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            demoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DemoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DemoDto demo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("测试字段1", demo.getTest1());
            map.put("创建时间", demo.getCreateTime());
            map.put("更新时间", demo.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}